utf8proc_uint8_t *output;
utf8proc_map_custom(input, 0, &output, UTF8PROC_CASEFOLD | UTF8PROC_COMPOSE | UTF8PROC_COMPAT | UTF8PROC_NULLTERM,
custom, &thunk_test);
- printf("mapped \"%s\" -> \"%s\"\n", (char*)input, (char*)output);
- check(strlen((char*) output) == 6, "incorrect output length");
- check(!memcmp(correct, output, 7), "incorrect output data");
- free(output);
+ check_compare("map_custom", input, correct, output, 1);
printf("map_custom tests SUCCEEDED.\n");
return 0;
}
utf8proc_uint8_t input[] = {0x72, 0xcc, 0x87, 0xcc, 0xa3, 0x00}; /* "r\u0307\u0323" */
utf8proc_uint8_t nfc[] = {0xe1, 0xb9, 0x9b, 0xcc, 0x87, 0x00}; /* "\u1E5B\u0307" */
utf8proc_uint8_t nfd[] = {0x72, 0xcc, 0xa3, 0xcc, 0x87, 0x00}; /* "r\u0323\u0307" */
- utf8proc_uint8_t *nfc_out, *nfd_out;
- nfc_out = utf8proc_NFC(input);
- printf("NFC \"%s\" -> \"%s\" vs. \"%s\"\n", (char*)input, (char*)nfc_out, (char*)nfc);
- check(strlen((char*) nfc_out) == 5, "incorrect nfc length");
- check(!memcmp(nfc, nfc_out, 6), "incorrect nfc data");
- nfd_out = utf8proc_NFD(input);
- printf("NFD \"%s\" -> \"%s\" vs. \"%s\"\n", (char*)input, (char*)nfd_out, (char*)nfd);
- check(strlen((char*) nfd_out) == 5, "incorrect nfd length");
- check(!memcmp(nfd, nfd_out, 6), "incorrect nfd data");
- free(nfd_out); free(nfc_out);
+
+ check_compare("NFC", input, nfc, utf8proc_NFC(input), 1);
+ check_compare("NFD", input, nfd, utf8proc_NFD(input), 1);
}
-static void issue102(void) /* #128 */
+static void issue102(void) /* #102 */
{
utf8proc_uint8_t input[] = {0x58, 0xe2, 0x81, 0xa5, 0x45, 0xcc, 0x80, 0xc2, 0xad, 0xe1, 0xb4, 0xac, 0x00}; /* "X\u2065E\u0300\u00ad\u1d2c" */
utf8proc_uint8_t stripna[] = {0x78, 0xc3, 0xa8, 0x61, 0x00}; /* "x\u00e8a" */
utf8proc_uint8_t correct[] = {0x78, 0xe2, 0x81, 0xa5, 0xc3, 0xa8, 0x61, 0x00}; /* "x\u2065\u00e8a" */
utf8proc_uint8_t *output;
+
utf8proc_map(input, 0, &output, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
UTF8PROC_COMPOSE | UTF8PROC_COMPAT | UTF8PROC_CASEFOLD | UTF8PROC_IGNORE | UTF8PROC_STRIPNA);
- printf("NFKC_Casefold \"%s\" -> \"%s\" vs. \"%s\"\n", (char*)input, (char*)output, (char*)stripna);
- check(strlen((char*) output) == 4, "incorrect NFKC_Casefold+stripna length");
- check(!memcmp(stripna, output, 5), "incorrect NFKC_Casefold+stripna data");
- free(output);
- output = utf8proc_NFKC_Casefold(input);
- printf("NFKC_Casefold \"%s\" -> \"%s\" vs. \"%s\"\n", (char*)input, (char*)output, (char*)correct);
- check(strlen((char*) output) == 7, "incorrect NFKC_Casefold length");
- check(!memcmp(correct, output, 8), "incorrect NFKC_Casefold data");
- free(output);
+ check_compare("NFKC_Casefold+stripna", input, stripna, output, 1);
+ check_compare("NFKC_Casefold", input, correct, utf8proc_NFKC_Casefold(input), 1);
}
int main(void)
{
if (!cond) {
va_list args;
- fprintf(stderr, "line %zd: ", lineno);
+ if (lineno)
+ fprintf(stderr, "FAILED at line %zd: ", lineno);
+ else
+ fprintf(stderr, "FAILED: ");
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
buf[i] = 0;
return i;
}
+
+void print_escaped(FILE* f, const utf8proc_uint8_t *utf8) {
+ fprintf(f, "\"");
+ while (*utf8) {
+ utf8proc_int32_t codepoint;
+ utf8 += utf8proc_iterate(utf8, -1, &codepoint);
+ if (codepoint < 0x10000)
+ fprintf(f, "\\u%04x", codepoint);
+ else
+ fprintf(f, "\\U%06x", codepoint);
+ }
+ fprintf(f, "\"");
+}
+
+void print_string_and_escaped(FILE* f, const utf8proc_uint8_t *utf8) {
+ fprintf(f, "\"%s\" (", (const char *) utf8);
+ print_escaped(f, utf8);
+ fprintf(f, ")");
+}
+
+void check_compare(const char *transformation,
+ const utf8proc_uint8_t *input, const utf8proc_uint8_t *expected,
+ utf8proc_uint8_t *received, int free_received) {
+ int passed = !strcmp((const char *) received, (const char *) expected);
+ FILE *f = passed ? stdout : stderr;
+ fprintf(f, "%s: %s ", passed ? "PASSED" : "FAILED", transformation);
+ print_string_and_escaped(f, input);
+ fprintf(f, " -> ");
+ print_string_and_escaped(f, received);
+ if (!passed) {
+ fprintf(f, " != expected ");
+ print_string_and_escaped(f, expected);
+ }
+ fprintf(f, "\n");
+ if (free_received) free(received);
+ if (!passed) exit(1);
+}